package com.gwz.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gwz.sys.constast.SYSConstast;
import com.gwz.sys.domain.Permission;
import com.gwz.sys.mapper.PermissionMapper;
import com.gwz.sys.service.PermissionService;
import com.gwz.sys.utils.DataGridView;
import com.gwz.sys.vo.PermissionVo;

@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {

	@Autowired
	private PermissionMapper permissionMapper;
	
	@Override
	public List<Permission> queryAllPermissionForList(PermissionVo permissionVo) {
		return permissionMapper.queryAllPermission(permissionVo);
	}

	@Override
	public List<Permission> queryPermissionByUserIdForList(PermissionVo permissionVo, Integer userId) {
		permissionVo.setType(SYSConstast.PERMISSION_TYPE_MEUN);
		return permissionMapper.queryPermissionByUserId(SYSConstast.PERMISSION_TYPE_MEUN,userId);
	}

	@Override
	public DataGridView queryAllPermission(PermissionVo permissionVo) {
		Page<Object> page = PageHelper.startPage(permissionVo.getPage(), permissionVo.getLimit());
		List<Permission> data = permissionMapper.queryAllPermission(permissionVo);
		return new DataGridView(page.getTotal(), data);
	}

	@Override
	public Integer queryMaxOrderNumber() {
		return permissionMapper.queryMaxOrderNumber();
	}

	@Override
	public void addPermission(PermissionVo permissionVo) {
		permissionMapper.insertSelective(permissionVo);
	}

	@Override
	public void updatePermission(PermissionVo permissionVo) {
		permissionMapper.updateByPrimaryKeySelective(permissionVo);
	}

	@Override
	public Integer isCurrentPermissionHasChildrenNode(Integer id) {
		return permissionMapper.isCurrentPermissionHasChildrenNode(id);
	}

	@Override
	public void deletePermission(PermissionVo permissionVo) {
		permissionMapper.deleteByPrimaryKey(permissionVo.getId());
	}

	@Override
	public List<Permission> queryPermisionByRoleIdForList(PermissionVo permissionVo, Integer id) {
		return permissionMapper.queryPermisionByRoleIdForList(id);
	}

	@Override
	public List<String> queryPemissionByUserId(Integer userId) {
		List<Permission> list = permissionMapper.queryPermissionByUserId(SYSConstast.PERMISSION_TYPE_PERMISSION,userId);
		List<String> permissions = new ArrayList<>();
		for (Permission p : list) {
			permissions.add(p.getPercode());
		}
		return permissions;
	}

}
